package org.yxing.palpalgps.chat;

import java.security.KeyStoreException;
import java.util.Map;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.yxing.palpalgps.domain.UserInfo;
import org.yxing.palpalgps.utils.ConfigHelper;

public class Connection {
    
    public static final String HOST = "palpalgps.lovebus.me"; 
    public static final String SERVICE_NAME = "palpalgps.lovebus.me";
    public static final String RESOURCE = "palpalgps";
    
    private UserInfo currentUser;
    private XMPPConnection conn = null;
    
    private static Connection instance = null;
    
    public Map<String, Chat> chats;
    
    private Connection() {
        currentUser = ConfigHelper.nowUser;
    }
    
    public static Connection getInstance() {
        if (instance == null) {
            instance = new Connection();
        }
        return instance;
    }
    
    
    public XMPPConnection getConn() throws XMPPException {
        if (null == conn) {
            ConnectionConfiguration conf = new ConnectionConfiguration(HOST, 5222, SERVICE_NAME);
            conn = new XMPPConnection(conf);
            conn.connect();
        }
        return conn;
    }
    
    public void login() throws XMPPException {
        conn.login(currentUser.getId(), currentUser.getChatPassword(), RESOURCE);
    }
}
